package com.yjd.api.project;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.IntroDetailModel;
import com.yjd.comm.base.service.ICityService;
import com.yjd.comm.base.service.IIntroDetailService;
import com.yjd.comm.doctor.model.DoctorModel;
import com.yjd.comm.doctor.service.IDoctorService;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.project.model.PrjCategoryModel;
import com.yjd.comm.project.service.IPrjCategoryService;
import com.yjd.comm.project.service.IProjectService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ResultMapBuilder;
import com.yjd.comm.util.ServletUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目相关内容处理
 * Created by yangchangyan on 2017/6/27.
 */
@Controller("com.yjd.api.project.ProjectControl")
@Scope("prototype")
@RequestMapping("/project")
public class ProjectControl extends BaseControl {

    @Resource
    private IProjectService projectService;

    @Resource
    private IIntroDetailService introDetailService;

    @Resource
    private IDoctorService doctorService;

    @Resource
    private ICityService cityService;

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private IPrjCategoryService prjCategoryService;

    /**
     * 查询热门项目
     * Created by yangchangyan on 2017/6/28.
     *
     * @param page 查询第page页
     * @param rows 每页展示rows条数据
     * @return void
     * @throws
     */
    @RequestMapping(value = "/hotList", method = RequestMethod.POST)
    public void hotProject(int page, int rows) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);

        paramMap.put("sort", "(reservation_count+view_count) DESC");
        paramMap.put("hotProject", projectService.queryProject(paramMap));
        paramMap.remove("sort"); // 过滤条件不需要返回

        this.successData(paramMap);
    }

    /**
     * 查询项目详情
     * Created by yangchangyan on 2017/6/28.
     *
     * @param project_id 项目id
     * @return void
     * @throws
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public void projectDetail(Long project_id) throws Exception {

        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);

        // 查询项目基本信息
        List<Map<String, Object>> project = projectService.queryProject(paramMap);
        if (0 == project.size()) {
            FrameUtil.throwBaseException1("项目不存在");
        }

        // 浏览项目详情时，项目view_count++，用于热门项目统计
        projectService.updateByMap(FrameUtil.newHashMap("project_id", project_id, "is_used", "1", "view_count", "1"));

        // 构建返回信息
        Map<String, Object> projectResult = project.get(0);

        // 查询项目详情
        IntroDetailModel intro = introDetailService.getModelById(projectResult.get("detail_id"), DbWREnums.READ);
        projectResult.put("detail", null == intro ? "" : intro.getContent());

        // 查询项目负责医生基本信息
        DoctorModel doctor = doctorService.getModelById(projectResult.get("doctor_id"), DbWREnums.READ);
        Map<String, Object> doctorResult = ResultMapBuilder.build(doctor);
        if (null != doctor) {
            OrganizationModel doctorOrg = organizationService.getModelById(doctor.getOrg_id(), DbWREnums.READ);
            doctorResult.put("org_name", null == doctorOrg ? "" : doctorOrg.getOrg_name());
        }

        projectResult.put("doctorInfo", doctorResult);

        paramMap.put("project", projectResult);
        this.successData(paramMap);
    }

    /**
     * 根据地区查询项目，支持分页查询
     * Created by yangchangyan on 2017/7/04.
     *
     * @param regionId 地区ID
     * @param page     查询第page页
     * @param rows     每页展示rows条数据
     * @throws
     */
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    public void allProject(Long regionId, int page, int rows) throws Exception {
        // 如果地区id没有，则默认返回全国范围的机构分布信息
        if (null == regionId) {
            regionId = Long.valueOf(0L);
        }
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);

        // 返回全国范围的项目分布信息（即调用/project/all接口）
        if (0 == regionId) {
            paramMap.put("project", projectService.queryProject(paramMap));
            this.successData(paramMap);
            return;
        }

        // 查询所有regionId下面的所有三级城市id
        List<Long> ids = new ArrayList<Long>();
        ids = cityService.querySubCities(regionId, ids);

        // 查询ids城市中的机构
        paramMap.put("org_city_ids", ids);
        List<OrganizationModel> orgs = organizationService.getModelList(paramMap, DbWREnums.READ);
        paramMap.remove("org_city_ids");
        if (orgs.size() == 0) {
            paramMap.put("project", ArrayUtils.EMPTY_OBJECT_ARRAY);
            this.successData(paramMap);
            return;
        }

        // 更加机构id查询出对应的医生
        ids.clear();
        for (OrganizationModel org : orgs) {
            ids.add(org.getOrg_id());
        }
        paramMap.put("org_ids", ids);
        paramMap.put("project", projectService.queryProject(paramMap));
        paramMap.remove("org_ids");

        this.successData(paramMap);
    }

    /**
     * 项目分类列表
     * Created by yangchangyan on 2017/7/12.
     *
     * @throws
     */
    @RequestMapping(value = "/category")
    public void projectCategory() throws Exception {
        List<PrjCategoryModel> projectCgy = prjCategoryService.getModelList(null, DbWREnums.READ);
        PrjCategoryModel first = new PrjCategoryModel();
        first.setId(0L);
        first.setName("全部");
        projectCgy.add(0, first);
        this.successData(FrameUtil.newHashMap("cgyList", projectCgy));
    }

}
